home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / jumpMenuUI.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  7.2 KB  |  254 lines

  1. //
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3. //
  4. //jumpMenuUI.js
  5. //
  6. //Set of functions for use with the Jump Menu object
  7. //and Jump Menu behavior, and who knows, other files might
  8. //access as well in the future
  9. //
  10. //--------------------------------------------------------------
  11. //
  12.  
  13. //function:populateMenuOptions
  14. //description: populates the Menu Options form field
  15. //based on the contents of GarrMenuOptions. Overloaded to
  16. //behave differently depending on number of arguments.
  17. //arguments:
  18. //none - updates entire form field
  19. //otherwise, updates only the indexes passed as arguments
  20. //for instance populateMenuOptions(2) updates the third listed option,
  21. //populateMenuOptions(2,3) updates the third and fourth.
  22.  
  23. function populateMenuOptions(){
  24.    var opArr = GarrMenuOptions; //easier to deal with a short name
  25.    var argArr = populateMenuOptions.arguments;
  26.    var nArgs = argArr.length;
  27.    var i;
  28.    
  29.    if (nArgs == 0){ //if no args, update all options
  30.       var nOptions = GarrMenuOptions.length;
  31.       var optionLabel;
  32.       
  33.       for (i=0;i<nOptions;i++){
  34.          GlistOptions.options[i] = new Option();
  35.          optionLabel = createOptionLabel(opArr[i][0],opArr[i][1]);
  36.          GlistOptions.options[i].text = optionLabel;
  37.       }
  38.       
  39.    } 
  40.    else { //if args, only update specified options
  41.    
  42.       var currArg;
  43.       var newLabel="";
  44.  
  45.       for (i=0;i<nArgs;i++){
  46.          currArg = argArr[i];
  47.          newLable = createOptionLabel( opArr[currArg][0],opArr[currArg][1] );
  48.          GlistOptions.options[currArg].text = newLable;             
  49.       }  
  50.    }
  51. }
  52.  
  53. //function: isAllWhite
  54. //description: given a text string, determines if there are any non-whitespace
  55. //characters 
  56. //Its used because if an URL is only whitespace, we don't want to display it as:
  57. //Label(  ), we want instead to not use the (), which is confusing here.
  58. //returns: boolean
  59.  
  60. function isAllWhite(str){
  61.    var isAllWhite = true,i,c,counter=0;
  62.    
  63.    while (  str.charAt(counter)  ){
  64.       c = str.charAt(counter++);
  65.       if ( c != ' ' && c != '\t' && c != '\n' && c != '\r' ){
  66.          isAllWhite = false;
  67.          break;
  68.       }
  69.    }
  70.   
  71.    return isAllWhite;    
  72. }
  73.  
  74. //function: createOptionLabel
  75. //description: passed option text and option URL, generates
  76. //the text string that appears in Menu Options form field
  77. function createOptionLabel(Text,URL){
  78.    if (  URL && !isAllWhite(URL)  )
  79.       return (  Text + "  (" + URL + ")"  );
  80.    else
  81.       return Text ;
  82. }
  83.  
  84. //function addOption
  85. function addOption(){
  86.    var newInd = GarrMenuOptions.length;
  87.  
  88.    if (newInd==0 || GarrMenuOptions[newInd-1][0]) {
  89.      GarrMenuOptions[newInd] = new Array(getUniqueLabel(),"");
  90.      GlistOptions.options[newInd] = new Option(GarrMenuOptions[newInd][0]);
  91.      GlistOptions.selectedIndex = newInd;
  92.      GtfLabel.value = GarrMenuOptions[newInd][0];
  93.      GtfURL.value = GarrMenuOptions[newInd][1];
  94.    }
  95.    GtfLabel.focus();
  96.    GtfLabel.select();
  97. }
  98.  
  99. //function switchArrayEntries
  100. //description: acts on GarrMenuOptions only ( not UI )
  101. //switches the specified indexes
  102. //called from moveOptionUp() and moveOptionDown();
  103.  
  104. function switchArrayEntries(arr,ind1,ind2){
  105.    var temp = arr[ind1];
  106.    arr[ind1] = arr[ind2];
  107.    arr[ind2] = temp;
  108.    //arrays are passed by reference and not by value so
  109.    //there is no need to return anything
  110. }
  111.  
  112. //function deleteOption
  113. function deleteOption(){
  114.  
  115.    //easier to deal with a shorter name
  116.    var opArr = GarrMenuOptions; 
  117.    
  118.    var nOptions = opArr.length;
  119.    var selInd = GlistOptions.selectedIndex;
  120.    
  121.    //update global array
  122.    if (selInd > 0){ //if the first option is not selected
  123.       if (selInd!=opArr.length-1){ //if last option isn't selected
  124.          opArr = opArr.slice(0,selInd).concat( opArr.slice(selInd+1) );
  125.       } else{ //last option is selected
  126.          opArr = opArr.slice(0,-1);
  127.          selInd--;
  128.       }
  129.    } else { //first option is selected
  130.        if (opArr.length > 1){  //if there is more than one item in list
  131.          opArr = opArr.slice(1);
  132.        } else{  //if there is only one item in list
  133.          opArr[0] = new Array("","");
  134.        }
  135.    }
  136.    //delete extra (empty) option now available in Menu Options field
  137.    //i.e.: delete the selected option
  138.    GarrMenuOptions = opArr;
  139.    populateMenuOptions(); //populate UI with updated global option array
  140.    if (  nOptions!=1  ){
  141.       GlistOptions.options[nOptions-1] = null;
  142.    } 
  143.    if (  nOptions==1 || nOptions==2  ){
  144.       GtfLabel.focus();
  145.    }
  146.    GlistOptions.selectedIndex = selInd;
  147.    //update Label & URL fields to selected option Label & URL
  148.    GtfLabel.value = GarrMenuOptions[selInd][0];
  149.    GtfURL.value   = GarrMenuOptions[selInd][1];
  150.    
  151. }
  152.  
  153.  
  154. //function moveOptionUp
  155. function moveOptionUp(){
  156.  
  157.    var currInd = GlistOptions.selectedIndex;
  158.    var switchInd = currInd-1;
  159.    
  160.    //return if this is the top option
  161.    if (currInd == 0)
  162.       return;
  163.     
  164.    //update global array and re-populate Menu Options field
  165.    switchArrayEntries(GarrMenuOptions,currInd,switchInd); //update array
  166.    populateMenuOptions(currInd,switchInd);  //update UI
  167.    
  168.    //select new location
  169.    GlistOptions.selectedIndex = switchInd;
  170.    showOption(switchInd);
  171. }
  172.  
  173.  
  174. //function moveOptionDown
  175. function moveOptionDown(){
  176.  
  177.    var currInd = GlistOptions.selectedIndex;
  178.    var switchInd = currInd+1;
  179.  
  180.    //return if this is the bottom most option - stupid user!
  181.    if (currInd == GarrMenuOptions.length-1)
  182.       return;
  183.  
  184.    //update global array and re-populate Menu Options field
  185.    //based on new global array
  186.    switchArrayEntries(GarrMenuOptions,currInd,switchInd); //update array
  187.    populateMenuOptions(currInd,switchInd); //update UI
  188.    
  189.    //select new location
  190.    GlistOptions.selectedIndex = switchInd;
  191.    showOption(switchInd);
  192. }
  193.  
  194. //function: showOption
  195. //description: shows Label and URL values when a selection is clicked
  196. //in Menu Options list
  197. function showOption(){
  198.    var currInd = GlistOptions.selectedIndex;
  199.    GtfLabel.value = (GarrMenuOptions[currInd][0])?GarrMenuOptions[currInd][0]:"";
  200.    GtfURL.value   = (GarrMenuOptions[currInd][1])?GarrMenuOptions[currInd][1]:"";
  201.  
  202. }
  203.  
  204. //function updateOption
  205. //updates global array and re-writes to screen
  206. //attached to onBlur of Label and URL fields as:
  207. //updateOptionValues(this.name);
  208. //and to Browse button as:
  209. //updateOptionValues("URL");
  210. //
  211. //argument: "URL" or "Label"
  212.  
  213. function updateOption(whatToUpdate){
  214.    var currInd = GlistOptions.selectedIndex;
  215.    var updateInd = 0;
  216.    var newTextStr = "";
  217.    
  218.    if (whatToUpdate == "Label"){
  219.       newTextStr = GtfLabel.value;
  220.    } else {
  221.       updateInd = 1;
  222.       newTextStr = GtfURL.value;
  223.       if (GtfLabel.value.indexOf(TEXT_defaultItemText)==0) { //if default label, change to simple file name
  224.         var startPos = newTextStr.lastIndexOf("/");
  225.         var endPos = newTextStr.lastIndexOf(".");
  226.         if (endPos > 0) {
  227.           GtfLabel.value = newTextStr.substring(startPos+1,endPos);
  228.           updateOption('Label');
  229.         }
  230.       }
  231.    }
  232.    GarrMenuOptions[currInd][updateInd] = newTextStr;
  233.    populateMenuOptions(currInd);
  234. }
  235.  
  236.  
  237.  
  238. function browseClicked() {
  239.   browseFile(GtfURL);
  240.   updateOption('URL');
  241. }
  242.  
  243. function getUniqueLabel() {
  244.   var label, i, num=1, isUnique;
  245.  
  246.   for (isUnique=false; !isUnique; num++) {
  247.     label = TEXT_defaultItemText + num;
  248.     isUnique = true;
  249.     for (i=0; i<GarrMenuOptions.length && isUnique; i++)
  250.       if (GarrMenuOptions[i][0] == label) isUnique=false;
  251.   }
  252.   return label;
  253. }
  254.